package com.wlr.study.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * 二叉树展开为链表
 *
 * @author lrwang
 * @version 1.0
 * @date 2022/4/7 22:00
 */
public class LeetCode114 {
    public void flatten(TreeNode root) {
        List<TreeNode> list = new ArrayList<TreeNode>();
        becomeLinked(root, list);
        int size = list.size();
        for (int i = 1; i < size; i++) {
            TreeNode prev = list.get(i - 1), curr = list.get(i);
            prev.left = null;
            prev.right = curr;
        }
    }

    public void becomeLinked(TreeNode cur, List<TreeNode> list) {
        if (cur != null) {
            list.add(cur);
            becomeLinked(cur.left, list);
            becomeLinked(cur.right, list);
        }
    }

    public static void main(String[] args) {

    }
}
